from rest_framework import serializers
from apps.oaauth.models import OAAbstractUser
from django.core.validators import FileExtensionValidator


class AddstaffSerializer(serializers.Serializer):
    realname = serializers.CharField(max_length=100,required=True)
    email = serializers.EmailField(required=True)
    password = serializers.CharField(max_length=255,required=True)
    def validate(self, attrs):
        request = self.context.get('request')
        email  = attrs.get('email')
        if OAAbstractUser.objects.filter(email=email).exists():
            raise serializers.ValidationError('邮箱已存在')

        # 验证等前用户是否是部门的leader
        if request.user.department.leader.id != request.user.id:
            raise serializers.ValidationError('您不是部门领导，无法添加员工')
        return attrs

class UploadStaffSerializer(serializers.Serializer):
    file =  serializers.FileField(
        validators=[FileExtensionValidator(allowed_extensions=['xlsx', 'xls'])],
        error_messages={
            'required': '请上传文件',
        }
    )

